var ws = null;
var base64Img = '', videoImg = ''
//初始化--链接服务
export function oConnetSocket(callback, basee64Callback) {
  console.log('初始化');
  //注意:同一个ws才能正常操作视频
  ws = new WebSocket('ws://127.0.0.1:9187');
  ws.onopen = function () {
    console.log('客户端已连接');
    oOpenCamera();
    //document.all['returnMsg'].value = "客户端已连接"
  }
  ws.onmessage = function (evt) {
    //oUl.innerHTML+="<li>"+"recv"+"</li>";
    /*var imageStr="<img src='data:image/jpeg;base64,"+evt.data+"' style='width: 400px; height: 300px'></img>"
    $(outimage).html(imageStr);*/

    var jsonObj = JSON.parse(evt.data);
    //获取摄像头的数据,添加到选择框
    if (jsonObj.hasOwnProperty("command")) {
      if (jsonObj.command == "GetCamera") {
        var videoList = document.getElementById("cameraDevice");
        var devices = new Array();
        devices = jsonObj["devices"];
        $("#cameraDevice").empty();
        for (i = 0; i < devices.length; i++) {
          var device1 = devices[i];
          var opt = document.createElement('option');
          opt.text = device1.name;
          opt.value = device1.device;
          try {
            videoList.push(opt, null); // standards compliant
          } catch (ex) {
            videoList.push(opt); // IE only
          }
        }
      }
      //打开摄像头后设置模式
      if (jsonObj.command == "OpenCamera" && jsonObj.result == "打开成功") {
        oSetVideoMode();
      }
    }

    if (!jsonObj.videoFrame) {
      console.log(evt.data)
      //document.all['returnMsg'].value = evt.data;
      if (jsonObj.hasOwnProperty("takePhotoBase64")) {
        base64Img = jsonObj.takePhotoBase64;
        basee64Callback(base64Img);
      }
      if (jsonObj.hasOwnProperty("signBase64")) {
        base64Img = jsonObj.signBase64;
        basee64Callback(base64Img);
      }

      if (jsonObj.hasOwnProperty("FPImgBase64")) {
        base64Img = jsonObj.FPImgBase64;
        basee64Callback(base64Img);
      }
      if (jsonObj.hasOwnProperty("data")) {
        if (jsonObj.data.headphoto) {
          base64Img = jsonObj.data.headphoto;
          basee64Callback(base64Img);
        }
      }
    }
    else {
      videoImg = "data:image/jpg;base64," + jsonObj.videoFrame;
      callback(videoImg);
    }
  }
  ws.onclose = function () {
    //document.all['returnMsg'].value = "客户端已断开!"
    console.log('客户端已断开!')
  };
  ws.onerror = function (evt) {
    if (evt.data == undefined) {
      // document.all['returnMsg'].value = "连接服务失败,请先启动服务!";
      console.log('连接服务失败,请先启动服务!')
    }
  };
};

//获取摄像头
export function oGetCamera() {
  if (ws) {
    var json = '{"command":"GetCamera"}';
    ws.send(json);
  }
}

//打开摄像头
export function oOpenCamera() {
  if (ws) {
    //传入GetCamera获取的device
    var device = 1;//高拍;
    var json = '{"command":"OpenCamera","arguments":{"device":' + device + '}}';
    ws.send(json);
  }
}

//关闭摄像头
export function oCloseCamera() {
  if (ws) {
    var json = '{"command":"CloseCamera"}';
    ws.send(json);
  }
}

//设置拍照模式
export function oSetVideoMode() {
  if (ws) {
    var videoMode = 4; //自动寻边
    var json = '{ "command":"SetVideoMode","arguments":{ "videoMode":' + videoMode + '}}';
    ws.send(json);
  }
}

//拍照
export function oTakephoto() {
  if (ws) {
    var json = '{ "command":"TakePhoto","arguments":{ "imageFormat":1}}';
    ws.send(json);
  }
}

//旋转1
export function oRotateVideo() {
  if (ws) {
    var json = '{"command":"RotateVideo","arguments":{"Direction":0}}';
    ws.send(json);
  }
}
